//
//  JDNewsFlashViewController.swift
//  DragonWallet
//
//  Created by volantPig on 2024/7/29.
//

import UIKit
import SwiftyJSON
/// 发布快讯
class JDNewsFlashViewController: JDBaseViewController {
    
    var bottomView: UIView?
    var backButton: UIButton?
    var releaseButton: UIButton?
    
    var myTableView: UITableView?
    
    var projectName: JDInputInfoView?
    var typeView: JDSelectedInfoView?
//    var contactsNameView: JDInputInfoView?
    var contactsPhoneView: JDInputInfoView?
    var signUpView :JDSwitchView?
    
    var messageInputCell: JDMessageInputCell?
    
    var imgNumber: Int = 0
    var imgButtonOne: UICloseButton?
    var imgButtonTwo: UICloseButton?
    var imgButtonThree: UICloseButton?
    
    var videoButton: UICloseButton?
    
    var imgOneUrlStr: String?
    var imgTwoUrlStr: String?
    var imgThreeUrlStr: String?
    var videoUrlPath: String?
    
    var classificationTitleList: [String]?
    var classificationCodeList: [String]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupConfig()
        self.setupUIElements()
        self.getBusinessNewsTypeInfo()
        
        // Do any additional setup after loading the view.
    }
    
    func setupConfig() {
        self.setupNavigationTitle("发布快讯")
        self.removeSeparatorLine()
        self.setupLeftBackItem()
    }
    
    func setupUIElements() {

        self.bottomView = UIView()
        self.view.addSubview(self.bottomView!)
        self.bottomView?.snp.makeConstraints({ make in
            make.left.right.bottom.equalTo(self.view)
            make.height.equalTo(104)
        })
        self.bottomView?.backgroundColor = .white
        
        self.backButton = UIButton(type: .custom)
        self.bottomView?.addSubview(self.backButton!)
        self.backButton?.snp.makeConstraints({ make in
            make.centerY.equalTo(self.bottomView!)
            make.left.equalTo(self.bottomView!).offset(12)
            make.right.equalTo(self.bottomView!.snp.centerX).offset(-5)
            make.height.equalTo(54)
        })
        self.backButton?.setTitle("返回", for: .normal)
        self.backButton?.setTitleColor(.black, for: .normal)
        self.backButton?.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        self.backButton?.backgroundColor = .init(hexString: "#F5F6F8")
        self.backButton?.layer.cornerRadius = 27
        self.backButton?.addTarget(self, action: #selector(backAction), for: .touchUpInside)
        
        self.releaseButton = UIButton(type: .custom)
        self.bottomView?.addSubview(self.releaseButton!)
        self.releaseButton?.snp.makeConstraints({ make in
            make.left.equalTo(self.bottomView!.snp.centerX).offset(5)
            make.centerY.equalTo(self.bottomView!)
            make.height.equalTo(54)
            make.right.equalTo(self.bottomView!).offset(-12)
        })
        self.releaseButton?.setTitle("确定发布", for: .normal)
        self.releaseButton?.setTitleColor(.white, for: .normal)
        self.releaseButton?.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        self.releaseButton?.backgroundColor = .init(hexString: "#FA6161")
        self.releaseButton?.layer.cornerRadius = 27
        self.releaseButton?.addTarget(self, action: #selector(releaseAction), for: .touchUpInside)
        
        
        self.myTableView = UITableView(frame: .zero, style: .plain)
        self.view.addSubview(self.myTableView!)
        self.myTableView?.snp.makeConstraints({ make in
            make.left.right.equalTo(self.view)
            make.bottom.equalTo(self.bottomView!.snp.top)
            make.top.equalTo(self.view).offset(statusBarHeight+44)
        })
        self.myTableView?.delegate = self
        self.myTableView?.dataSource = self
        self.myTableView?.register(UITableViewCell.self, forCellReuseIdentifier: "emptyCell")
        self.myTableView?.register(JDMessageInputCell.self, forCellReuseIdentifier: "messageInputCell")
        self.myTableView?.backgroundColor = .init(hexString: "#F5F6F8")
        self.myTableView?.separatorStyle = .none
    }
    
    func getBusinessNewsTypeInfo() {
        
        var params = [String: Any]()
        params["showAllFlag"] = 0
        
        JDNetWorkRequest(.getBusinessNewsType(params)) { responseStr in
         
            if let businessNewTypeModels = [JDBusinessNewsTypeModel].deserialize(from: responseStr, designatedPath: "") {
                
                self.classificationTitleList = businessNewTypeModels.map { model in
                    model?.name ?? ""
                }
                
                self.classificationCodeList = businessNewTypeModels.map { model in
                    model?.code ?? ""
                }
            }

        } failure: { error in
            
        } error: { errorCode in
            
        }
    }
    
    private func getTypeCode(_ selectedIdx: Int) -> String {
        return self.classificationCodeList?[selectedIdx] ?? "400"
    }
    
    @objc func releaseAction() {
        print("发布看板啊")
        
        if (self.typeView?.selectedIdx ?? 0) < 0 {
            // toast 请选择分类
            return
        }
        
//        {
//          "businessTypeCode": 0,
//          "name": "string",
//          "content": "string",
//          "videoUrl": "string",
//          "picUrl1": "string",
//          "picUrl2": "string",
//          "picUrl3": "string",
//          "registrationEnabledFlag": 0
//        }
        var params = [String: Any]()
        params["businessTypeCode"] = self.getTypeCode(self.typeView?.selectedIdx ?? 0)
        params["name"] = self.projectName?.inputStr()
        params["content"] = self.messageInputCell?.inputTextView?.text
        if (self.videoUrlPath?.count ?? 0) > 0 {
            params["videoUrl"] = self.videoUrlPath
        }
        if (self.imgOneUrlStr?.count ?? 0) > 0 {
            params["picUrl1"] = self.imgOneUrlStr
        }
        if (self.imgTwoUrlStr?.count ?? 0) > 0 {
            params["picUrl2"] = self.imgTwoUrlStr
        }
        if (self.imgThreeUrlStr?.count ?? 0) > 0 {
            params["picUrl3"] = self.imgThreeUrlStr
        }
        // 是否开启报名 0: 不开启   1: 开启
        params["registrationEnabledFlag"] = 0
        self.showLoading()
        JDNetWorkRequest(.releaseBusinessNews(params)) { responseStr in
            
            printLog("发布成功啦吗?\(responseStr)")
            self.dismissLoading()
            let commonModel = JDCommonModel.deserialize(from: responseStr, designatedPath: "")
            if commonModel?.code == "0" {
                self.view.toast(message: "发布成功")
                self.navigationController?.popViewController(animated: true)
            } else {
                self.view.toast(message: commonModel?.msg ?? "网络异常")
            }
            
        } failure: { error in
            self.dismissLoading()
            self.view.toast(message: error.localizedDescription)
        } error: { errorCode in
            self.dismissLoading()
            self.view.toast(message: "网络异常")
        }
        
        
//        var params = [String: Any]()
////        params["onlineSign"] = JDLoginModel.default.onlineSign
//        /// 301:租售 302:商务 303:招募 304:家政 305:综合
//        /// self?.typeView?.selectedIdx
//        params["bussicode"] = self.getTypeCode(self.typeView?.selectedIdx ?? 0)
//        // 看板内容
//        params["context"] = self.messageInputCell?.inputTextView?.text
//        // 视频
//        if (self.videoUrlPath?.count ?? 0) > 0 {
//            params["vidurl"] = self.videoUrlPath
//        }
//        if (self.imgOneUrlStr?.count ?? 0) > 0 {
//            params["picurl1"] = self.imgOneUrlStr
//        }
//        if (self.imgTwoUrlStr?.count ?? 0) > 0 {
//            params["picurl2"] = self.imgTwoUrlStr
//        }
//        if (self.imgThreeUrlStr?.count ?? 0) > 0 {
//            params["picurl3"] = self.imgThreeUrlStr
//        }
//        // 联系人
////        params["lxr"] = self.contactsNameView?.inputStr()
//        // 联系电话
//        params["phone"] = self.contactsPhoneView?.inputStr()
////        {"lxr":"Zhu","onlineSign":"0462557df1284d3e9f72d7d3059c0d9a","picurl3":"图片名称","picurl2":"图片名称","bussicode":"301","picurl1":"图片名称","phone":"15011111111","context":"Wo yao ce sho "}
//        JDNetWorkRequest(.releaseBoard(params)) { responseStr in
//
//            let commonModel = JDCommonModel.deserialize(from: responseStr, designatedPath: "")
//            if commonModel?.code == "0" {
//                self.navigationController?.popViewController(animated: true)
//                // MARK: toast 发布成功
//            }
//            print("发布看板，发布成功:\(responseStr)")
////        发布看板，发布成功:{"returnCode":"00","returnMsg":null,"body":null}
//
//        } failure: { error in
//
//            print("发布看板，发布失败")
//
//        } error: { errorCode in
//            print("发布看板，发布错误:\(errorCode)")
//        }
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

    @objc func selectedImgV(_ currentButton: UIButton) {
        
        self.openAlbumAlert {[weak self] selectedType, images in
            if selectedType == .photoCamera {
                guard let imgV = images.first else { return }
                if currentButton == self?.imgButtonOne {
                    DispatchQueue.main.async {
                        self?.imgButtonOne?.setImage(imgV, for: .normal)
                        self?.imgButtonTwo?.isHidden = false
                    }
                    self?.uploadImgV(imgV, { urlStrDic in
                        self?.imgOneUrlStr = JSON(urlStrDic?.values.first as Any).stringValue
                        print("第一张图片地址:\(urlStrDic)")
                    })
                } else if currentButton == self?.imgButtonTwo {
                    self?.imgButtonTwo?.setImage(imgV, for: .normal)
                    self?.imgButtonThree?.isHidden = false
                    self?.uploadImgV(imgV, { urlStrDic in
                        self?.imgTwoUrlStr = JSON(urlStrDic?.values.first as Any).stringValue
                        print("第二张图片地址:\(urlStrDic)")
                    })
                } else if currentButton == self?.imgButtonThree {
                    self?.imgButtonThree?.setImage(imgV, for: .normal)
                    self?.uploadImgV(imgV, { urlStrDic in
                        self?.imgThreeUrlStr = JSON(urlStrDic?.values.first as Any).stringValue
                        print("第三张图片地址:\(urlStrDic)")
                    })
                }
            }  else if selectedType == .photoAblum {
//                for (index, subImage) in images.enumerated() {
//                    self?.uploadImgV(subImage, { urlStrDic in
//                        if index == 0 {
//                            DispatchQueue.main.async {
//                                self?.imgButtonOne?.setImage(subImage, for: .normal)
//                                self?.imgButtonTwo?.isHidden = false
//                                self?.imgOneUrlStr = JSON(urlStrDic?.values.first as Any).stringValue
//                            }
//                        } else if index == 1 {
//                            Task {
//                                self?.imgButtonTwo?.setImage(subImage, for: .normal)
//                                self?.imgButtonThree?.isHidden = false
//                                self?.imgTwoUrlStr = JSON(urlStrDic?.values.first as Any).stringValue
//                            }
//                        } else if index == 2 {
//                            Task {
//                                self?.imgButtonThree?.setImage(subImage, for: .normal)
//                                self?.imgThreeUrlStr = JSON(urlStrDic?.values.first as Any).stringValue
//                            }
//                        }
//                    })
//                }
                // TODO: 这里需要处理
                guard let imgV = images.first else { return }
                if currentButton == self?.imgButtonOne {
                    DispatchQueue.main.async {
                        self?.imgButtonOne?.setImage(imgV, for: .normal)
                        self?.imgButtonTwo?.isHidden = false
                    }
                    self?.uploadImgV(imgV, { urlStrDic in
                        self?.imgOneUrlStr = JSON(urlStrDic?.values.first as Any).stringValue
                        print("第一张图片地址:\(urlStrDic)")
                    })
                } else if currentButton == self?.imgButtonTwo {
                    self?.imgButtonTwo?.setImage(imgV, for: .normal)
                    self?.imgButtonThree?.isHidden = false
                    self?.uploadImgV(imgV, { urlStrDic in
                        self?.imgTwoUrlStr = JSON(urlStrDic?.values.first as Any).stringValue
                        print("第二张图片地址:\(urlStrDic)")
                    })
                } else if currentButton == self?.imgButtonThree {
                    self?.imgButtonThree?.setImage(imgV, for: .normal)
                    self?.uploadImgV(imgV, { urlStrDic in
                        self?.imgThreeUrlStr = JSON(urlStrDic?.values.first as Any).stringValue
                        print("第三张图片地址:\(urlStrDic)")
                    })
                }
            }
        }
        
//        printLog("选择照片")
//        self.openImgagePickerVC()
//        self.pictureSelectedCompleted = {[weak self] imgV in
//            if currentButton == self?.imgButtonOne {
//                DispatchQueue.main.async {
//                    self?.imgButtonOne?.setImage(imgV, for: .normal)
//                    self?.imgButtonTwo?.isHidden = false
//                }
//                self?.uploadImgV(imgV, { urlStrDic in
//                    self?.imgOneUrlStr = JSON(urlStrDic?.values.first).stringValue
////                    "https://img2.baidu.com/it/u=2239350219,2012909916&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1066"//urlStr
//                })
//            } else if currentButton == self?.imgButtonTwo {
//                self?.imgButtonTwo?.setImage(imgV, for: .normal)
//                self?.imgButtonThree?.isHidden = false
//                self?.uploadImgV(imgV, { urlStrDic in
//                    self?.imgTwoUrlStr = JSON(urlStrDic?.values.first).stringValue
////                    "https://img0.baidu.com/it/u=2191392668,814349101&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1399"//urlStr
//                })
//            } else if currentButton == self?.imgButtonThree {
//                self?.imgButtonThree?.setImage(imgV, for: .normal)
//                self?.uploadImgV(imgV, { urlStrDic in
//                    self?.imgThreeUrlStr = JSON(urlStrDic?.values.first).stringValue
////                    "https://wx1.sinaimg.cn/mw690/b3e366e1gy1hrd8ptp3l7j20u00u00um.jpg"//urlStr
//                })
//            }
//        }
       
    }
    
    @objc func selectedMainBoardVideo() {
        self.selectedVideo()
        self.videoSelectedCompleted = { [weak self] urlStr, firstImg in

            let outputPath = NSHomeDirectory() + "/Documents/\(Date().timeIntervalSince1970).mp4"

            JDTools.transformMoive(inputPath: urlStr, outputPath: outputPath) { videoLoacelUrl in
                DispatchQueue.main.async {
                    self?.videoButton?.setImage(firstImg, for: .normal)
                }
                self?.uploadVideo(videoLoacelUrl, { [weak self] videoNetUrlPathDic in
                    self?.videoUrlPath = JSON(videoNetUrlPathDic?.values.first as Any).stringValue
                    printLog("视频地址密文:\(JSON(videoNetUrlPathDic?.values.first as Any).stringValue)")
                })
            }
        }
    }
    
    @objc func removeImgV(index: Int) {
        
        if index == 0 {
            if (self.imgThreeUrlStr?.count ?? 0) > 0 {
                // 有第三个
                if let imgTwo = self.imgButtonTwo?.currentImage {
                    self.imgOneUrlStr = self.imgTwoUrlStr
                    self.imgButtonOne?.setImage(imgTwo, for: .normal)
                }
                if let imgThree = self.imgButtonThree?.currentImage {
                    self.imgTwoUrlStr = self.imgThreeUrlStr
                    self.imgButtonTwo?.setImage(imgThree, for: .normal)
                }
                
                self.imgThreeUrlStr = nil
                self.imgButtonThree?.refrehPlaceholder()
                
            } else {
                // 没有第三个
                if (self.imgTwoUrlStr?.count ?? 0) > 0 {
                    // 有第二个
                    if let imgTwo = self.imgButtonTwo?.currentImage {
                        self.imgOneUrlStr = self.imgTwoUrlStr
                        self.imgTwoUrlStr = nil
                        self.imgButtonOne?.setImage(imgTwo, for: .normal)
                        self.imgButtonTwo?.refrehPlaceholder()
                        self.imgButtonThree?.isHidden = true
                    }
                } else {
                    // 没有第二个
                    self.imgOneUrlStr = nil
                    self.imgButtonOne?.refrehPlaceholder()
                    self.imgButtonTwo?.isHidden = true
                }
            }
        } else if index == 1 {
            
            // 有第三个   删除第二个   需要退一个位置
            if (self.imgThreeUrlStr?.count ?? 0) > 0 {
                
                if let imgThree = self.imgButtonThree?.currentImage {
                    self.imgTwoUrlStr = self.imgThreeUrlStr
                    self.imgThreeUrlStr = nil
                    self.imgButtonTwo?.setImage(imgThree, for: .normal)
                    self.imgButtonThree?.refrehPlaceholder()
                }
            } else {
                // 没有第三个，但是第三个  应该已经显示了
                self.imgButtonThree?.isHidden = true
                self.imgTwoUrlStr = nil
                self.imgButtonTwo?.refrehPlaceholder()
                
            }

        } else if index == 2 {
            self.imgThreeUrlStr = nil
            self.imgButtonThree?.refrehPlaceholder()
        }
    }
     
}

extension JDNewsFlashViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return  8
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.row == 3 {
            if self.messageInputCell == nil {
                self.messageInputCell = tableView.dequeueReusableCell(withIdentifier: "messageInputCell") as? JDMessageInputCell
                self.messageInputCell?.inputTitle?.text = "项目说明"
                self.messageInputCell?.inputTextView?.placeholder = "请输入项目说明"
                self.messageInputCell?.inputTextView?.snp.updateConstraints({ make in
                    make.height.equalTo(83)
                })
            }
            
            return self.messageInputCell!
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "emptyCell")
        
        for subV in cell?.contentView.subviews ?? [UIView]() {
            subV.removeFromSuperview()
        }
        cell?.contentView.backgroundColor = .init(hexString: "#F5F6F8")
                
        if indexPath.row == 0 {
            // 项目编号
//            let projectNumberLabel = UILabel()
//            cell?.contentView.addSubview(projectNumberLabel)
//            projectNumberLabel.text = "项目编号: 9383473793"
//            projectNumberLabel.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 14, weight: .medium), textColor: color("#333333"))
//            projectNumberLabel.snp.makeConstraints { make in
//                make.left.equalTo(cell!.contentView).offset(24)
//                make.centerY.equalTo(cell!.contentView)
//            }
            
        } else if indexPath.row == 1 {
            // 项目名称
            
            if self.projectName == nil {
                self.projectName = JDInputInfoView()
            }
            cell?.contentView.addSubview(self.projectName!)
            self.projectName?.snp.makeConstraints({ make in
                make.left.equalTo(cell!.contentView).offset(12)
                make.top.equalTo(cell!.contentView)
                make.right.equalTo(cell!.contentView).offset(-12)
                make.bottom.equalTo(cell!.contentView).offset(-10)
            })
            self.projectName?.refreshTextInfo("项目名称", "请输入项目名称")
            
        } else if indexPath.row == 2 {
            
            if self.typeView == nil {
                self.typeView = JDSelectedInfoView()
            }
            self.typeView?.snp.removeConstraints()
            cell?.contentView.addSubview(self.typeView!)
            self.typeView?.snp.makeConstraints({ make in
                make.left.equalTo(cell!.contentView).offset(12)
                make.top.equalTo(cell!.contentView)
                make.right.equalTo(cell!.contentView).offset(-12)
                make.bottom.equalTo(cell!.contentView).offset(-10)
            })
            self.typeView?.refreshTextInfo("项目分类", "请选择")
        } else if indexPath.row == 4 {
            
            let containerView = UIView()
            cell?.contentView.addSubview(containerView)
            containerView.snp.makeConstraints { make in
                make.left.equalTo(cell!.contentView).offset(12)
                make.right.equalTo(cell!.contentView).offset(-12)
                make.top.equalTo(cell!.contentView)
                make.bottom.equalTo(cell!.contentView).offset(-10)
            }
            containerView.backgroundColor = .white
            containerView.layer.cornerRadius = 8
            
            let cellTitle = UILabel()
            containerView.addSubview(cellTitle)
            cellTitle.snp.makeConstraints { make in
                make.left.equalTo(containerView).offset(12)
                make.top.equalTo(containerView).offset(12)
                make.height.equalTo(21)
            }
            cellTitle.text = "视频展示"
            cellTitle.font = UIFont.systemFont(ofSize: 15, weight: .semibold)
            cellTitle.textColor = color("#333333")
            
            let titleImgV = UIImageView()
            containerView.addSubview(titleImgV)
            titleImgV.snp.makeConstraints { make in
                make.left.equalTo(cellTitle.snp.right).offset(5)
                make.centerY.equalTo(cellTitle)
            }
            titleImgV.contentMode = .scaleToFill
            titleImgV.image = UIImage(named: "release_video")?.withRenderingMode(.alwaysOriginal)
            
            self.videoButton = UICloseButton(type: .custom)
            self.videoButton?.closeCompleted = { [weak self] in
                self?.videoUrlPath = nil
                self?.videoButton?.setImage(UIImage(named: "add_placeholder"), for: .normal)
            }
            containerView.addSubview(self.videoButton!)
            self.videoButton?.snp.makeConstraints({ make in
                make.left.equalTo(containerView).offset(12)
                make.bottom.equalTo(containerView).offset(-12)
                make.size.equalTo(CGSize(width: 69, height: 69))
            })
            self.videoButton?.setImage(UIImage(named: "add_placeholder"), for: .normal)
            self.videoButton?.imageView?.contentMode = .scaleToFill
            self.videoButton?.addTarget(self, action: #selector(selectedMainBoardVideo), for: .touchUpInside)
            
        } else if indexPath.row == 5 {
            
            let containerView = UIView()
            cell?.contentView.addSubview(containerView)
            containerView.snp.makeConstraints { make in
                make.left.equalTo(cell!.contentView).offset(12)
                make.right.equalTo(cell!.contentView).offset(-12)
                make.top.equalTo(cell!.contentView)
                make.bottom.equalTo(cell!.contentView).offset(-10)
            }
            containerView.backgroundColor = .white
            containerView.layer.cornerRadius = 8
            
            let cellTitle = UILabel()
            containerView.addSubview(cellTitle)
            cellTitle.snp.makeConstraints { make in
                make.left.equalTo(containerView).offset(12)
                make.top.equalTo(containerView).offset(12)
                make.height.equalTo(21)
            }
            cellTitle.text = "图片展示"
            cellTitle.font = UIFont.systemFont(ofSize: 15, weight: .semibold)
            cellTitle.textColor = color("#333333")
            
            let titleImgV = UIImageView()
            containerView.addSubview(titleImgV)
            titleImgV.snp.makeConstraints { make in
                make.left.equalTo(cellTitle.snp.right).offset(5)
                make.centerY.equalTo(cellTitle)
            }
            titleImgV.contentMode = .scaleToFill
            titleImgV.image = UIImage(named: "release_image")?.withRenderingMode(.alwaysOriginal)
            
            self.imgButtonOne = UICloseButton(type: .custom)
            self.imgButtonOne?.closeCompleted = { [weak self] in
                self?.removeImgV(index: 0)
            }
            cell?.contentView.addSubview(self.imgButtonOne!)
            self.imgButtonOne?.snp.makeConstraints({ make in
                make.left.equalTo(containerView).offset(12)
                make.bottom.equalTo(containerView).offset(-12)
                make.size.equalTo(CGSize(width: 69, height: 69))
            })
            self.imgButtonOne?.setImage(UIImage(named: "add_placeholder"), for: .normal)
            self.imgButtonOne?.imageView?.contentMode = .scaleToFill
            self.imgButtonOne?.addTarget(self, action: #selector(selectedImgV(_:)), for: .touchUpInside)
            
            self.imgButtonTwo = UICloseButton(type: .custom)
            self.imgButtonTwo?.closeCompleted = { [weak self] in
                self?.removeImgV(index: 0)
            }
            cell?.contentView.addSubview(self.imgButtonTwo!)
            self.imgButtonTwo?.snp.makeConstraints({ make in
                make.left.equalTo(self.imgButtonOne!.snp.right).offset(12)
//                make.centerX.equalTo(cell!.contentView)
                make.bottom.equalTo(containerView).offset(-12)
                make.size.equalTo(CGSize(width: 69, height: 69))
            })
            self.imgButtonTwo?.setImage(UIImage(named: "add_placeholder"), for: .normal)
            self.imgButtonTwo?.imageView?.contentMode = .scaleToFill
            self.imgButtonTwo?.addTarget(self, action: #selector(selectedImgV(_:)), for: .touchUpInside)
            self.imgButtonTwo?.isHidden = true
            
            self.imgButtonThree = UICloseButton(type: .custom)
            self.imgButtonThree?.closeCompleted = { [weak self] in
                self?.removeImgV(index: 0)
            }
            cell?.contentView.addSubview(self.imgButtonThree!)
            self.imgButtonThree?.snp.makeConstraints({ make in
//                make.right.equalTo(containerView).offset(-12)
                make.left.equalTo(self.imgButtonTwo!.snp.right).offset(12)
                make.bottom.equalTo(containerView).offset(-12)
                make.size.equalTo(CGSize(width: 69, height: 69))
            })
            self.imgButtonThree?.setImage(UIImage(named: "add_placeholder"), for: .normal)
            self.imgButtonThree?.imageView?.contentMode = .scaleToFill
            self.imgButtonThree?.addTarget(self, action: #selector(selectedImgV(_:)), for: .touchUpInside)
            self.imgButtonThree?.isHidden = true
            
        } else if indexPath.row == 6 {
            if self.signUpView == nil {
                self.signUpView = JDSwitchView()
                
            }
            self.signUpView?.snp.removeConstraints()
            cell?.contentView.addSubview(self.signUpView!)
            self.signUpView?.snp.makeConstraints({ make in
                make.left.equalTo(cell!.contentView).offset(12)
                make.top.equalTo(cell!.contentView)
                make.right.equalTo(cell!.contentView).offset(-12)
                make.bottom.equalTo(cell!.contentView).offset(-10)
            })
            self.signUpView?.refreshTextInfo("报名功能", "启用")
        } else if indexPath.row == 7 {
            
            let paddingLabel = JDPaddingLabel()
            paddingLabel.edgeInsets = UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12)
            cell?.contentView.addSubview(paddingLabel)
            paddingLabel.snp.makeConstraints { make in
                make.left.equalTo(cell!.contentView).offset(12)
                make.top.equalTo(cell!.contentView)
                make.right.equalTo(cell!.contentView).offset(-12)
                make.bottom.equalTo(cell!.contentView).offset(-10)
            }
            paddingLabel.backgroundColor = .white
            paddingLabel.text = "平台与发布人约定如下:\n  发布人对发布内容的瑕疵承担法律责任。对不适当的发布内容，平台拥有下架、修改、删除的权利；由此导致的损失及责任均由发布人自负。\n\n发布价格：15元/条\n发布时长：15天"
            paddingLabel.numberOfLines = 0
            paddingLabel.layer.cornerRadius = 8
            paddingLabel.font = UIFont.systemFont(ofSize: 14, weight: .regular)
            paddingLabel.textColor = .init(hexString: "#333333")
            paddingLabel.addTextLineSpace(spaceHeight: 8)
            
        }
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
                
        if indexPath.row == 0 {
            return 0
            return 52
        }
        
        if indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 6 {
            
            return 68
        }
        
        if indexPath.row == 4 || indexPath.row == 5 {
            return 134
        }
        
        if indexPath.row == 3 || indexPath.row == 7 {
            return UITableView.automaticDimension
        }
        return 12
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 2 {
            self.view.endEditing(true)
            
            self.onePickerViewDataSource = self.classificationTitleList ?? [String]()
            
            self.initOnePickerView()
            self.onePickerViewCompleted = {[weak self] selectedIdx in
                print("当前选中索引:\(selectedIdx)")
                self?.typeView?.setContentInfo(self?.onePickerViewDataSource[selectedIdx])
                self?.typeView?.selectedIdx = selectedIdx
            }
            
        }
    }
    
}

extension JDNewsFlashViewController {
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        self.view.endEditing(true)
    }
}
